import pymysql


def connect_mysql():
    connection = None
    try:
        # 建立数据库连接
        connection = pymysql.connect(
            host='192.168.10.10',
            user='root',
            port=3307,
            password='123456',
            database='saas_back',
            charset='utf8mb4'

        )

        # 创建游标对象
        with connection.cursor() as cursor:
            # 执行 SQL 查询
            sql = "SELECT * FROM users limit 10;"
            cursor.execute(sql)

            # 获取查询结果
            result = cursor.fetchall()
            for row in result:
                print(row)
            #
            # # 插入数据
            # insert_sql = "INSERT INTO your_table (column1, column2) VALUES (%s, %s)"
            # values = ('value1', 'value2')
            # cursor.execute(insert_sql, values)

            # 提交事务
            connection.commit()

    except pymysql.MySQLError as e:
        print(f"Error: {e}")
    finally:
        if connection :
            connection.close()





if __name__ == '__main__':
    connect_mysql()
